import axios from '@/utils/request';
import qs from 'qs';
const baseURL = process.env.VUE_APP_USER_API;
const loginURL = process.env.VUE_APP_LOGIN_API;

// 获取用户信息
const userInfo = () => {
  return axios.request({
    method: 'get',
    url: baseURL + '/user/getUserByToken',
    headers: {
      hideLoading: true //是否显示loading
    }
  });
};

// 获取用户公司列表
const userCompany = () => {
  return axios.request({
    method: 'get',
    url: baseURL + '/user/getUserCompany',
    headers: {
      hideLoading: true //是否显示loading
    }
  });
};

// 获取用户企业角色
const userRole = () => {
  return axios.get(baseURL + '/user/getUserCurrentRole');
};

// 设置用户头像
const setAvatar = data => {
  return axios.request({
    method: 'PUT',
    url: baseURL + '/user/setAvatar',
    data,
    headers: {
      loadingText: encodeURI('头像上传中，请稍候...'),
      'Content-Type': 'multipart/form-data'
    }
  });
};

// 修改手机号
const editMobile = data => {
  return axios.put(baseURL + '/user/editMobile', data);
};

// 获取验证码
const getMsgCode = params => {
  return axios.get(loginURL + '/user/getSmsCode', {
    params
  });
};

// 设置用户名
const setNickname = data => {
  return axios.put(baseURL + '/user/setNickname', data);
};

// 设置密码
const setPassword = data => {
  return axios.put(baseURL + '/user/setPassword', data);
};

// 实名认证
const crtification = data => {
  return axios.request({
    method: 'PUT',
    url: baseURL + '/user/realNameAuthentication',
    data: qs.stringify(data),
    headers: {
      loadingText: encodeURI('实名认证中，请稍候...'),
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
};

// 获取邮件验证码
const getEmailCode = params => {
  return axios.get(baseURL + '/user/getEmailCode', {
    params
  });
};

// 设置电子邮件
const setEmail = data => {
  return axios.put(baseURL + '/user/setEmail', data);
};

// 修改主题
const setTheme = data => {
  return axios.request({
    method: 'PUT',
    url: baseURL + '/user/setTheme',
    data: qs.stringify(data),
    headers: {
      loadingText: encodeURI('主题切换中，请稍候...'),
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
};

// 修改语言
const setLanguage = data => {
  return axios.request({
    method: 'PUT',
    url: baseURL + '/user/setLanguage',
    data: qs.stringify(data),
    headers: {
      loadingText: encodeURI('语言切换中，请稍候...'),
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
};

export {
  userInfo,
  userCompany,
  userRole,
  setAvatar,
  setNickname,
  editMobile,
  getMsgCode,
  setPassword,
  crtification,
  getEmailCode,
  setEmail,
  setTheme,
  setLanguage
};
